home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / ImageTk.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  4.8 KB  |  186 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: ImageTk.py,v 1.1.1.1 1998/08/18 13:07:56 sjoerd Exp $
  4. #
  5. # a Tk display interface
  6. #
  7. # History:
  8. # 96-04-08 fl    Created
  9. # 96-09-06 fl    Added getimage method
  10. # 96-11-01 fl    Rewritten, removed image attribute and crop method
  11. # 97-05-09 fl    Use PyImagingPaste method instead of image type
  12. # 97-05-12 fl    Minor tweaks to match the IFUNC95 interface
  13. # 97-05-17 fl    Support the "pilbitmap" booster patch
  14. # 97-06-05 fl    Added file= and data= argument to image constructors
  15. # 98-03-09 fl    Added width and height methods to Image classes
  16. # 98-07-02 fl    Use default mode for "P" images without palette attribute
  17. # 98-07-02 fl    Explicitly destroy Tkinter image objects
  18. #
  19. # Copyright (c) Secret Labs AB 1997-98.
  20. # Copyright (c) Fredrik Lundh 1996-97.
  21. #
  22. # See the README file for information on usage and redistribution.
  23. #
  24.  
  25. import Image, Tkinter
  26.  
  27. # --------------------------------------------------------------------
  28. # Check for optional "pilbitmap" booster patch
  29.  
  30. _pilbitmap_ok = None
  31.  
  32. def _pilbitmap_check():
  33.     global _pilbitmap_ok
  34.     if _pilbitmap_ok is None:
  35.         try:
  36.             im = Image.new("1", (1,1))
  37.             Tkinter.BitmapImage(data="PIL:%d" % im.im.id)
  38.             _pilbitmap_ok = 1
  39.         except:
  40.             _pilbitmap_ok = 0
  41.     return _pilbitmap_ok
  42.  
  43. # --------------------------------------------------------------------
  44. # PhotoImage
  45.  
  46. class PhotoImage:
  47.  
  48.     def __init__(self, image = None, size = None, **kw):
  49.  
  50.         # Tk compatibility: file or data
  51.     if image is None:
  52.         if kw.has_key("file"):
  53.         image = Image.open(kw["file"])
  54.         del kw["file"]
  55.         elif kw.has_key("data"):
  56.         from StringIO import StringIO
  57.         image = Image.open(StringIO(kw["data"]))
  58.         del kw["data"]
  59.  
  60.     if hasattr(image, "mode") and hasattr(image, "size"):
  61.         # got an image instead of a mode
  62.         mode = image.mode
  63.             if mode == "P":
  64.                 # palette mapped data
  65.                 image.load()
  66.         try:
  67.             mode = image.palette.mode
  68.         except AttributeError:
  69.             mode = "RGB" # default
  70.         size = image.size
  71.         kw["width"], kw["height"] = size
  72.     else:
  73.             mode = image
  74.         image = None
  75.  
  76.     if mode not in ["1", "L", "RGB", "RGBA"]:
  77.         mode = Image.getmodebase(mode)
  78.  
  79.     self.__mode = mode
  80.     self.__size = size
  81.     self.__photo = apply(Tkinter.PhotoImage, (), kw)
  82.     if image:
  83.         self.paste(image)
  84.  
  85.     def __del__(self):
  86.     name = self.__photo.name
  87.     self.__photo.name = None
  88.     try:
  89.         self.__photo.tk.call("image", "delete", name)
  90.     except:
  91.         pass # ignore internal errors
  92.  
  93.     def __str__(self):
  94.     return str(self.__photo)
  95.  
  96.     def width(self):
  97.     return self.__size[0]
  98.  
  99.     def height(self):
  100.     return self.__size[1]
  101.  
  102.     def paste(self, im, box = None):
  103.  
  104.     # convert to blittable
  105.         im.load()
  106.         image = im.im
  107.         if image.isblock() and im.mode == self.__mode:
  108.             block = image
  109.         else:
  110.             block = image.new_block(self.__mode, im.size)
  111.             image.convert2(block, image) # convert directly between buffers
  112.  
  113.         self.__photo.tk.call("PyImagingPhoto", self.__photo, block.id)
  114.  
  115.  
  116. # --------------------------------------------------------------------
  117. # BitmapImage
  118.  
  119. class BitmapImage:
  120.  
  121.     def __init__(self, image = None, **kw):
  122.  
  123.         # Tk compatibility: file or data
  124.     if image is None:
  125.             if kw.has_key("file"):
  126.                 image = Image.open(kw["file"])
  127.                 del kw["file"]
  128.             elif kw.has_key("data"):
  129.                 from StringIO import StringIO
  130.                 image = Image.open(StringIO(kw["data"]))
  131.                 del kw["data"]
  132.  
  133.     self.__mode = image.mode
  134.     self.__size = image.size
  135.  
  136.         if _pilbitmap_check():
  137.             # fast way (requires the pilbitmap booster patch)
  138.             image.load()
  139.             kw["data"] = "PIL:%d" % image.im.id
  140.             self.__im = image # must keep a reference
  141.         else:
  142.             # slow but safe way
  143.             kw["data"] = image.tobitmap()
  144.         self.__photo = apply(Tkinter.BitmapImage, (), kw)
  145.  
  146.     def __del__(self):
  147.     name = self.__photo.name
  148.     self.__photo.name = None
  149.     try:
  150.         self.__photo.tk.call("image", "delete", name)
  151.     except:
  152.         pass # ignore internal errors
  153.  
  154.     def width(self):
  155.     return self.__size[0]
  156.  
  157.     def height(self):
  158.     return self.__size[1]
  159.  
  160.     def __str__(self):
  161.     return str(self.__photo)
  162.  
  163.  
  164. # --------------------------------------------------------------------
  165. # Helper for the Image.show method.
  166.  
  167. def _show(image, title):
  168.  
  169.     import Tkinter
  170.  
  171.     class UI(Tkinter.Label):
  172.     def __init__(self, master, im):
  173.         if im.mode == "1":
  174.         self.image = BitmapImage(im, foreground="white")
  175.         else:
  176.         self.image = PhotoImage(im)
  177.         Tkinter.Label.__init__(self, master, image=self.image,
  178.         bg="black", bd=0)
  179.  
  180.     if not Tkinter._default_root:
  181.     raise IOError, "tkinter not initialized"
  182.     top = Tkinter.Toplevel()
  183.     if title:
  184.     top.title(title)
  185.     UI(top, image).pack()
  186.